package com.totoro.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.totoro.dao.BaseHibernateDao;
import com.totoro.dao.TutorialDao;
import com.totoro.domain.Project;
import com.totoro.domain.Tutorial;
/*
 * 指导老师与毕业设计关联类（设计指导关系）Service
 * 
 */
@Transactional
@Service
public class TutorialService extends BaseService<Tutorial, Integer>{
	
	private TutorialDao tutorialDao;

	@Autowired
	@Override
	public void setBaseDao(BaseHibernateDao<Tutorial, Integer> tutorialDao) {
		// TODO Auto-generated method stub
		this.baseDao = tutorialDao;
		this.tutorialDao = (TutorialDao) tutorialDao;
	}
	
	// 查询唯一指导关系
	public Tutorial uniqueTutorial(Object tutor,Object project){
		return this.tutorialDao.uniqueTutorial(tutor, project);
	}
	
	public Tutorial getAverageTutorialScoreByProject(Project project) {
		Collection<Tutorial> tutorials = project.getTutorial();
		int workLoad = 0;
		int basicAbility = 0;
		int workAbility = 0;
		int achievementLevel = 0;
		//是否指导老师都已录入指导成绩
		boolean state = true;
		
		//循环算出平均成绩
		for (Iterator iterator = tutorials.iterator(); iterator.hasNext();) {
			Tutorial tutorial = (Tutorial) iterator.next();
			if(tutorial.getWorkLoad() != null) {
				workLoad += tutorial.getWorkLoad();
			}
			if(tutorial.getBasicAbility() != null) {
				basicAbility += tutorial.getBasicAbility();
			}
			if(tutorial.getWorkAbility() != null) {
				workAbility += tutorial.getWorkAbility();
			}
			if(tutorial.getAchievementLevel() != null) {
				achievementLevel += tutorial.getAchievementLevel();
			}
			if(tutorial.getState() != null) {
				state = state && tutorial.getState();
			} else {
				state = false;
			}
		}
		int size = tutorials.size();
		Tutorial averageTutorial = new Tutorial();
		averageTutorial.setAchievementLevel(achievementLevel/size);
		averageTutorial.setBasicAbility(basicAbility/size);
		averageTutorial.setWorkAbility(workAbility/size);
		averageTutorial.setWorkLoad(workLoad/size);
		averageTutorial.setState(state);
		
		return averageTutorial;
	}
}
